package com.scau.os.extension;

import javafx.util.Pair;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

public class EventSystem {

    public static EventSystem instance=new EventSystem();
    private TreeMap<String,EasyDelegate> m=new TreeMap<>();
    private Hashtable<Object, List<IUnRegister>> unregistersBySource =new Hashtable<>();
    private List<IUnRegister>bufferToDelete=new ArrayList<>();


    public IUnRegister register(MyDelegate myDelegate){
        if(!m.containsKey(myDelegate.eventName)){
            m.put(myDelegate.eventName,new EasyDelegate(myDelegate.eventName));
        }
        System.out.println("RG"+myDelegate.eventName);
        return m.get(myDelegate.eventName).add(myDelegate);

    }
    public IUnRegister register(MyDelegate myDelegate,Boolean isPrint){
        if(!m.containsKey(myDelegate.eventName)){
            m.put(myDelegate.eventName,new EasyDelegate(myDelegate.eventName));
        }
        if(isPrint) System.out.println("RG"+myDelegate.eventName);
        return m.get(myDelegate.eventName).add(myDelegate);
    }

    /**
     * 根据事件名，注销所有事件名与给定事件名一致的事件
     * @param myDelegate
     * 要注销的事件名
     */
    public void unRegister(MyDelegate myDelegate){
        if(m.containsKey(myDelegate.eventName)){
            m.remove(myDelegate.eventName);
        }
    }
    public void SendEvent(String eventName,Object data){
        if(m.containsKey(eventName)){
            EasyDelegate easyDelegate=m.get(eventName);
            if(easyDelegate.size()>0){
                System.out.println("SE"+eventName);
                UnRegisterByBuffer();
                //easyDelegate.execute(data);
                synchronized (easyDelegate){
                    easyDelegate.execute(data);
                }
                UnRegisterByBuffer();
            }

        }
    }
    public void SendEvent(String eventName,Object data,boolean isPrint){
        if(m.containsKey(eventName)){
            EasyDelegate easyDelegate=m.get(eventName);
            if(easyDelegate.size()>0){
                if(isPrint) System.out.println("SE"+eventName);
                easyDelegate.execute(data);
            }
        }
    }
    public void addUnRegister(Object source,IUnRegister unRegister){
        if(unregistersBySource.containsKey(source)){
            unregistersBySource.get(source).add(unRegister);
        }else {
            unregistersBySource.put(source,new ArrayList<>());
            unregistersBySource.get(source).add(unRegister);
        }
    }
    public void UnRegisterAllBySource(Object source){
        if(unregistersBySource.containsKey(source)){
            for (IUnRegister i :
                    unregistersBySource.get(source)) {
                bufferToDelete.add(i);
            }
            unregistersBySource.remove(source);
        }
    }

    /**
     * 将事件的注销时间往后推迟
     */
    private void UnRegisterByBuffer(){
        if(bufferToDelete.size()!=0){
            for (IUnRegister i :
                    bufferToDelete) {
                i.UnRegister();
            }
            bufferToDelete.clear();
        }
    }
}
